import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { type Token } from '@/api/auth/types'

export const useTokenStore = defineStore("token", () => {
    const tokenInfo = ref<Token>({
        username: '',
        token: '',
    })
    const setToken = (token: Token) => {
        tokenInfo.value = token
    }
    /**清空token */
    const clearToken = () => {
        tokenInfo.value = {
            username: '',
            token: ''
        }
    }
    /**getter */
    const getUsername = computed(() => {
        return tokenInfo.value.username
    })
    const getToken = computed(() => {
        return tokenInfo.value.token
    })

    return { tokenInfo, setToken, getToken, getUsername, clearToken }


}, {
    persist: {
        enabled: true,
        key: "tokenStore",
        // 默认是 localStorage
        storage: sessionStorage,
    }
})

